import {
  // scanPostion,
  thyroid2C,
  artery2C,
  liver2C,
  spleen2C,
  urinary2C,
  arteryCali2C,
  thyroidCali2C,
  urinaryCali2C,
  liverCali2C,
  spleenCali2C
} from "@/utils";
export const arteryCAndPw = ['23','24','25','26','27','28','37','38','39','40','41','42'];
export const thyroidCAndPw = ['CrossRightLobeC','LongRightLobeC','CrossLeftLobeC','LongLeftLobeC'];
export const urinaryCAndPw = ['5012','5022'];

export const arterySort = ["左-颈总动脉横切面","左-窦部横切面","左-颈内及颈外动脉横切面","左-颈内及颈外动脉纵切面","左-颈总动脉纵切面",
"左-颈总动脉起始部纵切面","右-颈总动脉横切面","右-窦部横切面","右-颈内及颈外动脉横切面","右-颈内及颈外动脉纵切面","右-颈总动脉纵切面","右-颈总动脉起始部纵切面",
"左-颈总内中膜厚度","左-颈总PSV和EDV","右-颈总内中膜厚度","右-颈总PSV和EDV"]
export const thyroidSort = ["峡部横切面","左侧叶横切面","左侧叶纵切面","右侧叶横切面","右侧叶纵切面","峡部前后径","左侧叶前后径、左右径","右侧叶前后径、左右径",
"横切左侧甲状腺C模式","纵切左侧甲状腺C模式"];
export const urinarySort = ["左肾长轴切面","右肾长轴切面","左肾上下径、前后径","右肾上下径、前后径"];
export const liverSort = ["肝左叶经腹主动脉长轴纵切面","肝左叶斜切面","肋下斜切第二肝门切面","右肋下右肝及右肾纵切面","右肋间经第一肝门斜切面","胆囊长轴纵切面",
"胰腺长轴切面","肝左叶上下径、厚径(前后径)","肝右叶斜径","门静脉主干内径","胆囊长径、短径","胰体前后径"];
export const spleenSort = ["脾脏纵切面"];


//图片src转化为base64
export function getBase64Image(url) {
  return new Promise((resolve) => {
    const canvas = document.createElement("canvas");
    const img = new Image();
    img.setAttribute("crossOrigin", "anonymous");
    img.onload = function () {
      canvas.width = img.width;
      canvas.height = img.height;
      const ctx = canvas.getContext("2d");
      ctx.drawImage(img, 0, 0, img.width, img.height);
      const dataURL = canvas.toDataURL("image/png");
      resolve(dataURL);
    };
    img.src = url;
  });
}
//将base64转换为文件
export function dataURLtoFile(dataurl, filename = "zzz") {
  var arr = dataurl.split(","),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = window.atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], filename, { type: mime });
}

export function dellImgInfo(images, pos, model) {
  images.forEach((item) => {
    const lastPath = item.image ? item.image.split("/") : item.url.split("/");
    const lastPathLen = lastPath.length;
    const imgInfo = lastPath[lastPathLen - 1];
    let name = "";
    // let res = "";
    if (pos == "颈动脉" && model == "自主模式") {
      const imgInfoArr = imgInfo.split("-");
      if (imgInfoArr.length < 3) {
        item.info = "";
        item.status = "normal";
      } else {
        const dire = imgInfoArr[1] === "L" ? "左" : "右";
        name = artery2C[imgInfoArr[2]];
        if(!artery2C[imgInfoArr[2]] && imgInfoArr[2].includes("Default")){
          name = "疑似异常"
        }
        if(arteryCAndPw.includes(imgInfoArr[2])){
          item.status = "cali";
          name = arteryCali2C[imgInfoArr[2]] || artery2C[imgInfoArr[2]]
        }else {
          item.status = imgInfoArr[3] ? imgInfoArr[3] : "normal";
          name = imgInfoArr[3] == 'cali' ? arteryCali2C[imgInfoArr[2]] || artery2C[imgInfoArr[2]] : artery2C[imgInfoArr[2]]
        }
        // res = scanPostion[imgInfoArr[3]] ? scanPostion[imgInfoArr[3]] : "正常";
        item.info = imgInfoArr[2] ? `${dire}${name}` : "";
      }
    } else if (pos == "甲状腺" && model == "自主模式") {
      const imgInfoArr = imgInfo.split("_");
      if (imgInfoArr.length < 3) {
        item.info = "";
        item.status = "normal";
      } else {
        name = thyroid2C[imgInfoArr[0]] ? thyroid2C[imgInfoArr[0]] : null;
        if (imgInfo.includes("normal")) {
          // res = "正常";
          item.status = "normal";
        } else if (imgInfo.includes("cali") && !imgInfo.includes("Lesion")) {
          // res = "测量";
          item.status = "cali";
          name = thyroidCali2C[imgInfoArr[0]] || thyroid2C[imgInfoArr[0]]
        } else if (imgInfo.includes("Lesion")) {
          // res = "疑似异常图";
          item.status = "lesion";
        } else {
          // res = "正常";
          if(thyroidCAndPw.includes(imgInfoArr[0])){
            item.status = "cali";
            name = thyroidCali2C[imgInfoArr[0]] || thyroid2C[imgInfoArr[0]]
          }else{
            item.status = "normal";
          }
        }
        if(!thyroid2C[imgInfoArr[0]]&& imgInfoArr[0].includes("Default")){
          name = "疑似异常"
        }
        item.info = name ? `${name}` : "";
      }
    } else if ((pos == "腹部" || pos == "消化系") && model == "自主模式") {
      const imgInfoArr = imgInfo.split("-");
      if (imgInfoArr.length < 3) {
        item.info = "";
        item.status = "normal";
      } else {
        name = liver2C[imgInfoArr[2]] || spleen2C[imgInfoArr[2]];
        if(!liver2C[imgInfoArr[2]] && imgInfoArr[2].includes("Default") || !spleen2C[imgInfoArr[2]] && imgInfoArr[2].includes("Default") ){
          name = "疑似异常"
        }
        if(imgInfoArr[3] == "cali"){
          name = liverCali2C[imgInfoArr[2]] || spleenCali2C[imgInfoArr[2]] ||urinary2C[imgInfoArr[2]] || spleen2C[imgInfoArr[2]];
        }
        // res = scanPostion[imgInfoArr[3]] ? scanPostion[imgInfoArr[3]] : "正常";
        item.info = name ? `${name}` : "";
        item.status = imgInfoArr[3] ? imgInfoArr[3] : "normal";
      }
    } else if ((pos == "泌尿" || pos == "泌尿系") && model == "自主模式") {
      const imgInfoArr = imgInfo.split("-");
      if (imgInfoArr.length < 3) {
        item.info = "";
        item.status = "normal";
      } else {
        const dire = imgInfoArr[1] === "L" ? "左" : "右";
        name = urinary2C[imgInfoArr[2]];
        if(!urinary2C[imgInfoArr[2]] && imgInfoArr[2].includes("Default")){
          name = "疑似异常"
        }
        if(urinaryCAndPw.includes(imgInfoArr[2])){
          item.status = "cali";
          name = urinaryCali2C[imgInfoArr[2]] || urinary2C[imgInfoArr[2]];
        }else{
          if(imgInfoArr[3] == "cali"){
            name = urinaryCali2C[imgInfoArr[2]] || urinary2C[imgInfoArr[2]];
          }
          item.status = imgInfoArr[3] ? imgInfoArr[3] : "normal";
        }
        // res = scanPostion[imgInfoArr[3]] ? scanPostion[imgInfoArr[3]] : "正常";
        item.info = name ? `${dire}${name}` : "";
      }
    } else if (model == "远程模式" || model == "自主模式") {
      item.info = "";
      item.status = "normal";
    }
    if (!item.type) {
      item.type = "";
    }
    item.url = item.image || item.url;
    item.visable = true;
  });
}
//根据检查记录中的reportImg及measureInfo中的LesionInfo信息初始化每一张图片的智能结构化信息
export function dellReportImgInfo(images, pos, model, measureInfo) {
  try {
    images = images ? JSON.parse(images) : [];
    measureInfo = measureInfo ? JSON.parse(measureInfo) : {};
    const LesionInfo = measureInfo.LesionInfo ? measureInfo.LesionInfo : [];
    if (images.length > 0) {
      if (model == "自主模式") {
        const new_images = [];
        images.forEach((item) => {
          const lastPath = item.image
            ? item.image.split("/")
            : item.url.split("/");
          const lastPathLen = lastPath.length;
          const imgInfo = lastPath[lastPathLen - 1];
          const imgInfoArr =
            pos == "甲状腺" ? imgInfo.split("_") : imgInfo.split("-");
          let new_item = {
            ...item,
          };
          if (!item.tip) {
            if (pos == "颈动脉") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfo :", imgInfo);
                console.log("imgInfoArr.length < 3", imgInfoArr);
                new_item.tip = "正常";
              } else {
                if (imgInfo.includes("Lesion")) {
                  new_item.tip = "疑似局灶性异常";
                } else {
                  new_item.tip = "正常";
                }
              }
            } else if (pos == "甲状腺") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfo :", imgInfo);
                console.log("imgInfoArr.length < 3", imgInfoArr);
                new_item.tip = "正常";
              } else {
                if (imgInfo.includes("Lesion")) {
                  new_item.tip = "疑似局灶性异常";
                } else {
                  new_item.tip = "正常";
                }
              }
            } else if (pos == "腹部" || pos == "消化系") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfo :", imgInfo);
                console.log("imgInfoArr.length < 3", imgInfoArr);
                new_item.tip = "正常";
              } else {
                if (imgInfo.includes("Lesion")) {
                  new_item.tip = "疑似局灶性异常";
                } else {
                  new_item.tip = "正常";
                }
              }
            } else if (pos == "泌尿" || pos == "泌尿系") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfo :", imgInfo);
                console.log("imgInfoArr.length < 3", imgInfoArr);
                new_item.tip = "正常";
              } else {
                if (imgInfo.includes("Lesion")) {
                  new_item.tip = "疑似局灶性异常";
                } else {
                  new_item.tip = "正常";
                }
              }
            }
          }
          if (!item.local) {
            if (pos == "颈动脉") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfoArr.length < 3");
              } else {
                new_item.local =
                  imgInfoArr[1] === "L" ? "左侧颈总动脉" : "右侧颈总动脉";
              }
            } else if (pos == "甲状腺") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfoArr.length < 3");
              } else {
                if (LesionInfo.length > 0) {
                  const target = LesionInfo.find(
                    (i) => `${i.lesionId}.jpg` == item.param_id
                  );
                  if (target) {
                    new_item.local = target.loc;
                  } else {
                    new_item.local = "峡部";
                  }
                } else {
                  if (imgInfoArr[0].includes("Thmus")) {
                    new_item.local = "峡部";
                  } else if (imgInfoArr[0].includes("Right")) {
                    new_item.local = "右叶";
                  } else if (imgInfoArr[0].includes("Left")) {
                    new_item.local = "左叶";
                  } else {
                    new_item.local = "峡部";
                  }
                }
              }
            } else if (pos == "腹部" || pos == "消化系") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfoArr.length < 3");
              } else {
                let name = liver2C[imgInfoArr[2]] || spleen2C[imgInfoArr[2]];
                if (name && name.includes("肝")) {
                  new_item.local = "肝脏";
                } else if (name && name.includes("胆")) {
                  new_item.local = "胆囊";
                } else if (name && name.includes("胰")) {
                  new_item.local = "胰腺";
                } else if (name && name.includes("脾")) {
                  new_item.local = "脾脏";
                } else {
                  new_item.local = "肝脏";
                }
              }
            } else if (pos == "泌尿" || pos == "泌尿系") {
              if (imgInfoArr.length < 3) {
                console.log("imgInfoArr.length < 3");
              } else {
                new_item.local = imgInfoArr[1] === "L" ? "左肾" : "右肾";
              }
            }
          }
          new_images.push(new_item);
        });
        if (LesionInfo && LesionInfo.length > 0) {
          new_images.forEach((i) => {
            const target = LesionInfo.find(
              (item) => item.LesionId == i.param_id
            );
            if (target) {
              Object.assign(i, target);
            }
          });
        }
        return new_images;
      } else {
        return images;
      }
    } else {
      return images;
    }
  } catch (error) {
    console.log("dellReportImgInfo", error);
  }
}
//根据param_id删除LesionInfo中的信息
export function delLesionInfo(param_id, LesionInfo) {
  try {
    const new_LesionInfo = [...LesionInfo];
    const findIndex = new_LesionInfo.findIndex(
      (item) => item.LesionId == param_id
    );
    if (findIndex != -1) {
      new_LesionInfo.splice(findIndex, 1);
    }
    return new_LesionInfo;
  } catch (error) {
    console.log("delLesionInfo error", error);
  }
}
export function MergeArrays(arrA, arrB) {
  const idsInA = new Set(arrA.map((item) => item.id));
  const eleToAdd = arrB.filter((item) => !idsInA.has(item.id));
  return arrA.concat(eleToAdd);
}
export function dellVideoInfo(url,pos,magic2){
  let name = pos;
  if(pos == "泌尿"){
    name = "肾脏";
  }
  if(magic2 && pos == "腹部"){
    name = magic2 == "liver" ? "肝胆胰" : "脾脏";
  }
  const dir= url.includes("left") ? "左侧" : url.includes("right") ? "右侧": "";
  return `${dir}${name}`
}
export const sortMap = {
  "artery":arterySort,
  "thyroid":thyroidSort,
  "urinary":urinarySort,
  "liver":liverSort,
  "spleen":spleenSort,
}
export function customSortAndLabel(oriData,pos,type){
  console.log("oriData",oriData)
  let sortData;
  if(pos == "颈动脉"){
    sortData = sortMap["artery"]
  }else if(pos == "甲状腺"){
    sortData = sortMap["thyroid"]
  }else if(pos == "泌尿"){
    sortData = sortMap["urinary"]
  }else if(pos == "腹部"){
    if(type == 'liver'){
      sortData = sortMap["liver"]
    }else{
      sortData = sortMap["spleen"]
    }
  }
  console.log("sortData",sortData)
  if(!pos){
    return oriData;
  }
  const sortedArrRes = [];
  sortData.forEach((item,index)=>{
   let count = 1;
    oriData.forEach((i)=>{
      if(i.info.includes(item)){
        if(i.status == "normal"){
          const info = count > 1 ? `${index + 1}-${count}.${i.info}` : `${index + 1}.${i.info}`;
          const obj = {
            ...i,
            info
          }
          sortedArrRes.push(obj);
          count++;
        }else if(i.status == "cali"){
          sortedArrRes.push(i);
        }
      }
    })
  })
  console.log("sortedArrRes",sortedArrRes)
  const remainItems = oriData.filter(item=>!sortedArrRes.find(i=>i.id == item.id))
  console.log("remainItems",remainItems)
  return  sortedArrRes.concat(remainItems);
}
